package settings;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import database.AppDatabase;
import de.app.badresden.R;

public class SettingsActivity extends PreferenceActivity implements OnPreferenceClickListener{

	protected Bundle icicle;

	public void onCreate(Bundle icicle) {

		super.onCreate(icicle);
		
		getListView().setCacheColorHint(Color.TRANSPARENT);
		
		this.addPreferencesFromResource(R.xml.settings);

		Preference deletelogin = (Preference) findPreference("deletelogin");
		deletelogin.setOnPreferenceClickListener(this);
		
		Preference deletedata = (Preference) findPreference("delete_data");
		deletedata.setOnPreferenceClickListener(this);
		
		Preference stundenplanSetting = (Preference) findPreference("anzahl_importweeks");
		stundenplanSetting.setSummary(R.string.settings_datenstundenplansummary);
		stundenplanSetting.setTitle(R.string.settings_datenstundenplantitle);
		
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {

		AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
		dialogbuilder.setCancelable(false);

		if (preference.getKey().equals("deletelogin")) {

			dialogbuilder.setMessage(getString(R.string.settings_loginloeschendialog));
			dialogbuilder.setPositiveButton(getString(R.string.app_ja),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int id) {

							SharedPreferences preferences = PreferenceManager
									.getDefaultSharedPreferences(SettingsActivity.this);
							Editor edit = preferences.edit();
							edit.putString("matrikelnummer", "");
							edit.putString("passwort", "");
							edit.commit();
							finish();
							startActivity(getIntent());
						}
					});
		}

		if (preference.getKey().equals("delete_data")) {

			dialogbuilder.setMessage(getString(R.string.settings_datenloeschendialog));
			dialogbuilder.setPositiveButton(getString(R.string.app_ja),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int id) {

							AppDatabase provider = new AppDatabase(getApplicationContext());							
							provider.deleteData(provider.getWritableDatabase());

							finish();
							startActivity(getIntent());
						}
					});
		}

		dialogbuilder.setNegativeButton(getString(R.string.app_nein),
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int id) {

						dialog.cancel();
					}
				});

		AlertDialog dialog = dialogbuilder.create();
		dialog.show();
		return false;
	}
}
